"""
Help viewer.
"""
import wx, sys, os, wx.html
from util import *

HELP_BASE = os.path.join(os.path.dirname(__file__), 'help')

class HelpViewer(wx.Panel):
	"""
	Help viewer.
	"""
	def __init__(self, parent):
		super(HelpViewer, self).__init__(parent)

		self.tb = tb = wx.ToolBar(self, style = wx.TB_HORIZONTAL |
		                          wx.NO_BORDER | wx.TB_FLAT)
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(tb, 0, wx.EXPAND)

		tsize = (16, 16)
		tb.SetToolBitmapSize(tsize)

		home_bmp    = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR, tsize)
		back_bmp    = wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR, tsize)
		forward_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_TOOLBAR, tsize)

		tb.AddLabelTool(10, "Home", home_bmp,
		                shortHelp="Go to the table of contents.",
		                longHelp="Go to the table of contents.")
		self.Bind(wx.EVT_TOOL, self.show_home, id=10)

		tb.AddLabelTool(20, "Back", back_bmp,
		                shortHelp="Go to the last page you visited.",
		                longHelp="Go to the last page you visited.")
		self.Bind(wx.EVT_TOOL, self.go_back, id=20)

		tb.AddLabelTool(30, "Forward", forward_bmp,
		                shortHelp="Go to the next page you visited.",
		                longHelp="If you clicked the Back button, this " +
		                         "button returns you to the page you were on")
		self.Bind(wx.EVT_TOOL, self.go_forward, id=30)

		tb.Realize()

		self.help_view = hv = wx.html.HtmlWindow(self)
		sizer.Add(hv, 1, wx.ALL | wx.EXPAND)

		self.SetSizer(sizer)
		self.Layout()

		self.show_home()

	def show_home(self, event = None):
		self.help_view.LoadPage(os.path.join(HELP_BASE, 'index.html'))

	def go_back(self, event = None):
		# TODO: grey out the buttons when applicable.
		self.help_view.HistoryBack()

	def go_forward(self, event = None):
		self.help_view.HistoryForward()
